//
//  TestMainVIewController.swift
//  SunxianhuaTestApp
//
//  Created by abc on 2021/10/11.
//

import Foundation
import UIKit
import SKPhotoBrowser
class NewsTableVIewController: BaseViewController{
    
    var tableView :BaseTableVIew!
    private var viewPresent :NewsTableViewPresent = NewsTableViewPresent()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tabBarController?.tabBar.isHidden = false
        self.title = "新闻"
        tableView = BaseTableVIew.init().then({
            self.view.addSubview($0)
            $0.snp.makeConstraints { make in
                make.edges.equalTo(0.0)
            }
            $0.dataSource = self
            $0.delegate   = self
            $0.register(NewsTableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
            $0.separatorStyle = .none
        })
        viewPresent.delegate = self
        self.handleEvent()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false
    }
    
    
    private func handleEvent(){
        

        tableView.refreshDataBlock = {[weak self]() in
            
            print("加载新数据");
            self?.viewPresent.requestMoreData(isRefesh: true)
            self?.tableView.mj_header?.endRefreshing()
        }
        tableView.loardMoreDataBlock = {[weak self]() in
            print("获取更多");
            self?.viewPresent.requestMoreData()
        }
        
        viewPresent.requestMoreData(isRefesh: false)
        viewPresent.requestMoreData()
    }
    

    //跳转直接写到这里
    private func clickImageView(imgUrl :String?){
        
        guard let url = imgUrl else {return}
        let photos = [url].map { (item) -> SKPhoto in
            let photo = SKPhoto.photoWithImageURL(item)
            photo.shouldCachePhotoURLImage = false // you can use image cache by true(NSCache)
            return photo
        }
        let browser = SKPhotoBrowser(photos: photos)
        browser.initializePageIndex(0)
        self.present(browser, animated: true, completion: {})
    }
    

}


extension NewsTableVIewController :UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewPresent.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? NewsTableViewCell else {
            return UITableViewCell.init()
        }
        let model = viewPresent.dataArray[indexPath.row]
        cell.reloadCellWith(cellModel: model) {[weak self] in
            self?.clickImageView(imgUrl: model.urlToImage)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return viewPresent.dataArray[indexPath.row].sumHeight
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("点击了")
        let model = viewPresent.dataArray[indexPath.row]
        self.navigationController?.pushViewController(DetailWebViewController.init(url: model.url), animated: true)
    }
    
}

extension NewsTableVIewController :NewsTableViewModelDelegate{
    
    func reloadView(isHaveMoreData: Bool) {
        
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
        
    }
    
}




